package com.wcpdoc.survey.home.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wcpdoc.survey.core.dao.BaseDao;
import com.wcpdoc.survey.core.service.impl.BaseServiceImp;
import com.wcpdoc.survey.core.util.ValidateUtil;
import com.wcpdoc.survey.home.service.HomeSurveyService;
import com.wcpdoc.survey.survey.entity.PaperQuestionEx;
import com.wcpdoc.survey.survey.entity.Survey;
import com.wcpdoc.survey.survey.entity.SurveyAnswer;
import com.wcpdoc.survey.survey.entity.SurveyAnswerDetail;
import com.wcpdoc.survey.survey.service.PaperService;
import com.wcpdoc.survey.survey.service.SurveyAnswerDetailService;
import com.wcpdoc.survey.survey.service.SurveyAnswerService;
import com.wcpdoc.survey.survey.service.SurveyService;
import com.wcpdoc.survey.sys.cache.DictCache;

/**
 * 首页调查服务层实现
 * 
 * v1.0 zhanghc 2017年10月16日下午6:23:38
 */
@Service
public class HomeSurveyServiceImpl extends BaseServiceImp<Object> implements HomeSurveyService {
	@Resource
	private PaperService paperService;
	@Resource
	private SurveyService surveyService;
	@Resource
	private SurveyAnswerService surveyAnswerService;
	@Resource
	private SurveyAnswerDetailService surveyAnswerDetailService;
	
	@Override
	public void setDao(BaseDao<Object> dao) {
		
	}

	@Override
	public List<PaperQuestionEx> getPaperList(Integer paperId) {
		return paperService.getPaperList(paperId);
	}

	@Override
	public Survey getSurvey(Integer surveyId) {
		return surveyService.getEntity(surveyId);
	}

	@Override
	public void doPaper(Integer surveyId, String remoteIp, Map<Integer, String> answers) {
		//校验数据有效性
		Survey survey = getSurvey(surveyId);
		if(survey.getState() == 0){
			throw new RuntimeException("【"+survey.getName()+"】已"+DictCache.getDictValue("SURVEY_STATE", "0")+"！" );
		}
		if(survey.getState() == 2){
			throw new RuntimeException("【"+survey.getName()+"】"+DictCache.getDictValue("SURVEY_STATE", "1")+"！" );
		}
		if(survey.getEndTime().getTime() < new Date().getTime()){
			throw new RuntimeException("调查【"+survey.getName()+"】已结束！");
		}
		
		//添加调查答案
		SurveyAnswer surveyAnswer = new SurveyAnswer();
		surveyAnswer.setIp(remoteIp);
		surveyAnswer.setUpdateTime(new Date());
		surveyAnswer.setSurveyId(surveyId);
		surveyAnswerService.save(surveyAnswer);
		
		//添加调查答案详细
		for(Entry<Integer, String> entry : answers.entrySet()){
			if(!ValidateUtil.isValid(entry.getValue())){
				continue;
			}
			SurveyAnswerDetail surveyAnswerDetail = new SurveyAnswerDetail();
			surveyAnswerDetail.setQuestionId(entry.getKey());
			surveyAnswerDetail.setAnswer(entry.getValue());
			surveyAnswerDetail.setSurveyId(survey.getId());
			surveyAnswerDetail.setPaperId(survey.getPaperId());
			surveyAnswerDetail.setSurveyAnswerId(surveyAnswer.getId());
			surveyAnswerDetailService.save(surveyAnswerDetail);
		}
	}
}
